const requestUrl = require('../../../config').requestUrl
//API 操作
let serviceData = require('../../../datas/service.js')

import * as echarts from '../../../ec-canvas/echarts';

 
let that = null;
 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ecPie: {},
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,  // tab切换  
    qyArr: [] //企业列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

     that = this;
    var self = this

    // 获取系统信息 
    wx.getSystemInfo({
      success: function (res) {
        self.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    this.ecPieComponent = this.selectComponent('#mychart-dom-pie');

    this.GetQyArrCount();
  },

 
   
  //获取企业列表
  GetQyArrCount: function () {

    var self = this

    let param = {
      API_URL: requestUrl + "interGzhByqiyeCount.action",
      data: {
        "organName": wx.getStorageSync("OrganName")
      }
    };

    console.log(param);
    serviceData.result(param).then(data => {

      console.log('接口GetQyArrCount请求成功');
      console.log(data);

      this.setData({
        qyArr: data.data,
      })

      self.PieInit();

    }).catch(e => {
      console.log(e)
    })
  },

  /** 
 * 滑动切换tab 
*/
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },

  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    }
    else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  // 点击按钮后初始化图表
  PieInit: function () {
    this.ecPieComponent.init((canvas, width, height) => {
      // 获取组件的 canvas、width、height 后的回调函数
      // 在这里初始化图表
      const chartPie = echarts.init(canvas, null, {
        width: width,
        height: height
      });

      getPieOption(chartPie);

      // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
      this.chartPie = chartPie;

      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chartPie;
    });
  },


});

function getPieOption(chartPie) {

  var qyArr = that.data.qyArr;

  console.log(qyArr.length);

  var qyData = [];
  var qyName = [];
 

  for (var i = 0; i < qyArr.length; i++) {
    qyName.push(

      qyArr[i].qiyename
    );

    qyData.push(
      { value: qyArr[i].count,name:qyArr[i].qiyename}
    );

  }

  console.log(qyData);

  const option = {
    backgroundColor: "#ffffff",
    color: ["#37A2DA", "#32C5E9", "#67E0E3", "#91F2DE", "#FFDB5C", "#FF9F7F"],
    // grid: {
    //   show: true,
    //   borderColor: '#19507c',
    //   bottom:60,
    //   height:60
    // },
 
    legend: {
      type: 'scroll',
      data: qyName,
      bottom: 'bottom',
      left: 'center',
      bottom: 10,
      // backgroundColor: 'red',
      z: 100
    },
    tooltip: {
      trigger: 'item',
      formatter: "{b} : {c} ({d}%)"
    },
    series: [{
      label: {
        normal: {
          position: 'inner',
          formatter: '{c}个 ({d}%)',
          fontSize: 6
        }
      },
      type: 'pie',
      center: ['50%', '50%'],
      radius: ['30%', '80%'],
      data: qyData,
      itemStyle: {
        emphasis: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 2, 2, 0.3)'
        }
      }
    }]
  };

  console.log(option)

  chartPie.setOption(option);

}